package d2p

import (
	"hy_808/protocol/hygprs"
	"hy_808/protocol/jtt808"
	"time"
)

var (
	AlarmSpeedExceed uint32 = 0x02
	AlarmFatigue     uint32 = 0x04 //疲劳
	//AlarmAssisted    uint32 = 0x08 //辅助驾驶报警 808的预警
	AlarmTest uint32 = 0x2f
	AlarmAdas uint32 = 0x20000000
	Status    uint32 = 0x03
)

type Location struct {
	jtt808.Jtt808
	AlarmId   uint32 //报警标识
	AttachId  byte   //附加类型ID 0x65 疲劳  0x64 辅助驾驶adas
	Attach    []byte //报警附加信息
	Status    uint32 //状态
	Lat       uint32 //纬度 以度为单位的纬度值乘以 10 的 6 次方， 精确到百万分之一度
	Lon       uint32 //经度  以度为单位的纬度值乘以 10 的 6 次方， 精确到百万分之一度
	Altitude  uint16 //海拔高度  海拔高度，单位为米(m
	Speed     uint16 //速度  1/10km/h
	Direction uint16 //0-359,正北为 0，顺时针
	TheTime   string // 时间 6字节 bcd码 YY-MM-DD-hh-mm-ss(GMT+8 时间，本 标准之后涉及的时间均采用此时区
	data      []byte
	dataLen   int
}

func (this *Location) SetAlarmId() {

}

func (this *Location) SetAlarmAttach(attachId byte) {
	this.AttachId = attachId
}

// 获取报警附加数据
func (this *Location) GetAlarmAttach() []byte {
	var b []byte
	if len(this.Attach) > 0 {
		attachId := this.AttachId     //驾驶员状态报警
		attachLen := len(this.Attach) //附加信息长度
		b = []byte{byte(attachId), byte(attachLen)}
		b = append(b, this.Attach...)
	}

	return b
}

func (this *Location) GetAlarmIdB() []byte {
	return jtt808.FromUint32(this.AlarmId)
}

func (this *Location) GetStatusB() []byte {
	return jtt808.FromUint32(this.Status)
}

// 经度
func (this *Location) GetLatB() []byte {
	return jtt808.FromUint32(this.Lat)
}

// 纬度
func (this *Location) GetLonB() []byte {
	return jtt808.FromUint32(this.Lon)
}

// 海拔高程
func (this *Location) GetAltitudeB() []byte {
	return jtt808.FromUint16(this.Altitude)
}

// 速度
func (this *Location) GetSpeedB() []byte {
	return jtt808.FromUint16(this.Speed)
}

// 方向
func (this *Location) GetDirectionB() []byte {
	return jtt808.FromUint16(this.Direction)
}

// 时间
func (this *Location) GetTheTimeB() []byte {
	t := jtt808.Number2bcd(this.TheTime)
	return t
}

func (this *Location) SetBodyData() {
	this.data = hygprs.SlicesAppend(
		this.GetAlarmIdB(),
		this.GetStatusB(),
		this.GetLonB(), //纬度
		this.GetLatB(), //经度
		this.GetAltitudeB(),
		this.GetSpeedB(),
		this.GetDirectionB(),
		this.GetTheTimeB(),
		this.GetAlarmAttach(),
	)
	//检验实际长度
	if len(this.data) != this.dataLen {
		//log.Println("实际长度异常")
	}
}

func (this *Location) GetBodyData() []byte {
	return this.data
}

/*
*
 */
func GenLocation(busId uint32, phone string, AlarmId, Status, Lon, Lat uint32, Altitude, Speed, Direction uint16, TheTime time.Time, attatcId byte, attach []byte) Location {
	t := TheTime.Local().Format("060102150405")
	lo := Location{
		AlarmId:   AlarmId,
		Status:    Status,
		AttachId:  attatcId,
		Attach:    attach,
		Lat:       Lat,
		Lon:       Lon,
		Altitude:  Altitude,
		Speed:     Speed,
		Direction: Direction,
		TheTime:   t,
	}
	lo.dataLen = 28
	lo.SetBodyData()
	bodyData := lo.GetBodyData()
	basicLen := len(bodyData)
	attachLen := 0
	bodyLen := basicLen + attachLen
	lo.SetBody(bodyData)

	lo.Head.SetId(jtt808.LocationReport)
	lo.Head.SetAttribute(int16(bodyLen), 0, 0)
	lo.Head.SetPhone(phone)
	seq := jtt808.GetSerialNumber()
	lo.Head.SetSeriaNo(seq)
	lo.SetCheckCode() //校验码
	lo.ToBytes()
	return lo
}
